// pages/edit/edit.ts
import { getPublicPaddingTop } from '../../utils/page'
import { AGE_RANGE, HEIGHT_RANGE, WEIGHT_RANGE } from '../../config/index'
import { getPersonalInformation, userEdit, getUserFieldConfig } from '../../utils/controller'
import { isNull } from '../../utils/util'
import {UploadFileUtil} from '../../utils/request'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    paddingTop: getPublicPaddingTop(),
    AGE_RANGE,
    HEIGHT_RANGE,
    WEIGHT_RANGE,
    self_info: '',
    marry_expect: '',
    wechat: '',
    // id:'',
    // phone:'',
    head_img:'',
    nickname:'',
    // tags_id:'',
    age: 0,
    height: 0,
    weight: 0,
    resident_address: '',
    domicile_address: '',
    attr: 0,
    sex: 0,
    work: '',
    degree: 0,
    annual_income: 0,
    is_house: 0,
    is_car: 0,
    is_marry: 0,
    is_mate: 0,
    is_feast: 0,
    is_cert: 0,
    child_plan: 0,
    marry_plan: 0,
    relatives_plan: 0,
    is_cohabit: 0,
    userConfig:{}
  },
  goBack(){
    //   wx.switchTab({
    //     url:"/pages/home/home"
    //  })
    
      wx.navigateBack()
    },
  // 滚动条滚到底部
  scrollBottom(){
    let that = this;
    if(this.data.self_info && this.data.marry_expect && this.data.wechat && this.data.head_img && this.data.age && this.data.height && this.data.weight && this.data.resident_address && this.data.domicile_address && this.data.attr && this.data.sex && this.data.work && this.data.degree && this.data.annual_income && this.data.is_house && this.data.is_car && this.data.child_plan && this.data.marry_plan && this.data.relatives_plan && this.data.is_cohabit){
      return 
    }
   
    
    wx.createSelectorQuery().select('#viewCommunicationBody').boundingClientRect(function (rect) {
      wx.pageScrollTo({
        scrollTop: rect.height,
        duration: 100 // 滑动速度
      })
      // that.setData({
      //   scrollTop: rect.height - that.data.scrollTop
      // });
    }).exec();
  },
  // 获取属性配置
  async getUserFieldConfig() {
    
    const result = await getUserFieldConfig()

    this.setData({ userConfig: result })
  },

  handleAgeChange(e) {
    console.log(e)
    this.setData({ age: e.detail.value })
    this.scrollBottom()
  },

  handleHeightChange(e) {
    this.setData({ height: HEIGHT_RANGE[e.detail.value] })
    this.scrollBottom()
  },

  handleWeightChange(e) {
    this.setData({ weight: WEIGHT_RANGE[e.detail.value] })
    this.scrollBottom()
  },

  handleSexChange(e) {
    const sex = +e.currentTarget.dataset.sex
    this.setData({ sex })
    this.scrollBottom()
  },

  handleAttrChange(e) {
    const attr = +e.currentTarget.dataset.attr
    this.setData({ attr })
    this.scrollBottom()
  },

  handleWorkChange(e) {
    this.setData({ work: e.detail.value })
    this.scrollBottom()
  },

  handleDegreeChange(e) {
    this.setData({ degree: +e.currentTarget.dataset.degree })
    this.scrollBottom()
  },

  handleAnnualIncomeChange(e) {
    this.setData({ annual_income: +e.currentTarget.dataset.income })
    this.scrollBottom()
  },

  bindResidentAddressChange(e) {
    console.log(e.detail.value[0])
    this.setData({ resident_address: e.detail.value[0] })
    this.scrollBottom()
  },
  bindDomicileAddressChange(e) {
    console.log(e.detail.value[0])
    this.setData({ domicile_address: e.detail.value[0] })
    this.scrollBottom()
  },

  handleIsHouseChange(e) {
    this.setData({ is_house: +e.currentTarget.dataset.house })
    this.scrollBottom()
  },
  handleIsCarChange(e) {
    this.setData({ is_car: +e.currentTarget.dataset.car })
    this.scrollBottom()
  },

  handleIsMarryChange(e) {
    this.setData({ is_marry: +e.currentTarget.dataset.ismarry })
    this.scrollBottom()
  },

  handleIsMateChange(e) {
    this.setData({ is_mate: +e.currentTarget.dataset.mate })
    this.scrollBottom()
  },

  handleIsFeastChange(e) {
    this.setData({ is_feast: +e.currentTarget.dataset.feast })
    this.scrollBottom()
  },

  handleIsCertChange(e) {
    this.setData({ is_cert: +e.currentTarget.dataset.cert })
    this.scrollBottom()
  },

  handleChildPlanChange(e) {
    this.setData({ child_plan: +e.currentTarget.dataset.child })
    this.scrollBottom()
  },

  handleMarryPlanChange(e) {
    this.setData({ marry_plan: +e.currentTarget.dataset.marryplan })
    this.scrollBottom()
  },

  handleRelativesPlanChange(e) {
    this.setData({ relatives_plan: +e.currentTarget.dataset.relative })
    this.scrollBottom()
  },

  handleIsCohabitChange(e) {
    this.setData({ is_cohabit: +e.currentTarget.dataset.cohabit })
    this.scrollBottom()
  },

  handleSelfInfoChange(e) {
    this.setData({ self_info: e.detail.value })
    this.scrollBottom()
  },

  handleMarryExpectChange(e) {
    this.setData({ marry_expect: e.detail.value })
    this.scrollBottom()
  },

  handleWechatChange(e) {
    this.setData({ wechat: e.detail.value })
    this.scrollBottom()
  },

  async saveInfo() {
    const _this = this
    // 校验
    if (!_this.data.sex) {
      wx.showToast({ icon: 'none', title: '请选择性别' })
      return
    }
    if (!_this.data.age) {
      wx.showToast({ icon: 'none', title: '请选择年龄' })
      return
    }
    if (!_this.data.height) {
      wx.showToast({ icon: 'none', title: '请选择身高' })
      return
    }
    if (!_this.data.weight) {
      wx.showToast({ icon: 'none', title: '请选择体重' })
      return
    }
    if (!_this.data.attr) {
      wx.showToast({ icon: 'none', title: '请选择属性' })
      return
    }
    if (!_this.data.work) {
      wx.showToast({ icon: 'none', title: '请输入职业' })
      return
    }
    if (!_this.data.annual_income) {
      wx.showToast({ icon: 'none', title: '请选择年收入' })
      return
    }
    if (!_this.data.resident_address) {
      wx.showToast({ icon: 'none', title: '请选择常居地' })
      return
    }
    if (!_this.data.domicile_address) {
      wx.showToast({ icon: 'none', title: '请选择户籍地' })
      return
    }
    if (!_this.data.is_house) {
      wx.showToast({ icon: 'none', title: '请选择房产情况' })
      return
    }
    if (!_this.data.is_car) {
      wx.showToast({ icon: 'none', title: '请选择车产情况' })
      return
    }
    if (!_this.data.is_marry) {
      wx.showToast({ icon: 'none', title: '请选择婚姻情况' })
      return
    }
    if (!_this.data.is_mate) {
      wx.showToast({ icon: 'none', title: '请选择伴侣情况' })
      return
    }
    if (!_this.data.is_feast) {
      wx.showToast({ icon: 'none', title: '请选择办酒席情况' })
      return
    }
    if (!_this.data.is_cert) {
      wx.showToast({ icon: 'none', title: '请选择是否办真证' })
      return
    }
    if (!_this.data.child_plan) {
      wx.showToast({ icon: 'none', title: '请选择孩子计划' })
      return
    }
    if (!_this.data.marry_plan) {
      wx.showToast({ icon: 'none', title: '请选择计划结婚时间' })
      return
    }
    if (!_this.data.relatives_plan) {
      wx.showToast({ icon: 'none', title: '请选择亲戚情况' })
      return
    }
    if (!_this.data.is_cohabit) {
      wx.showToast({ icon: 'none', title: '请选择是否同居' })
      return
    }

    if (!_this.data.wechat) {
      wx.showToast({ icon: 'none', title: '请填写微信号' })
      return
    }
    const {
      self_info,
      wechat,
      sex,
      work,
      attr,
      age,
      height,
      weight,
      annual_income,
      domicile_address,
      resident_address,
      marry_expect,
      is_house,
      is_marry,
      is_mate,
      is_feast,
      is_cohabit,
      is_cert,
      is_car,
      head_img,
      child_plan,
      relatives_plan,
      marry_plan,
      degree,
      nickname
    } = _this.data;
    let data = {
      self_info,
      wechat,
      sex,
      work,
      attr,
      age,
      height,
      weight,
      annual_income,
      domicile_address,
      resident_address,
      marry_expect,
      is_house,
      is_marry,
      is_mate,
      is_feast,
      is_cohabit,
      is_cert,
      is_car,
      head_img,
      child_plan,
      relatives_plan,
      marry_plan,
      degree,
      nickname
    };
    const res: any = await userEdit(data)
    console.log({res});
    if(res.code == 10000){
      wx.showToast({
        icon:'none',
        title: res.msg,
      })
    }else{
      wx.showToast({
        icon:'none',
        title: res.msg,
      })
    }
    
    // wx.showLoading({ title: '完善信息中..', mask: true })

    // setTimeout(function () {
    //   wx.hideLoading()
    // }, 3000)
  },
  uploadimg(){
    var that = this;
    wx.chooseMedia({
      count: 9, //表示可以选择的最大图片数量限制，这里设置为 9 表示最多可以选择 9 张图片
      mediaType: ['image'], //表示媒体文件的类型，在这里设置为 ['image'] 表示只能选择图片类型的文件
      sizeType: ['original'], // 是否压缩所选文件
      sourceType: ['album', 'camera'], //表示可以选择的媒体来源，这里设置为 ['album', 'camera'] 表示可以从相册或相机进行选择
      camera: 'back', //表示前后摄像头的选择，默认为后置摄像头。当然设置的就是后置摄像头
      success:async (res) =>  {
        let img = res.tempFiles[0]['tempFilePath'];
        
        let url:any = await UploadFileUtil(img)
        that.setData({
          head_img:url
        })
        that.scrollBottom()
      }
    })

   
  },
  handleNickname(e){
    this.setData({ nickname: e.detail.value })
    this.scrollBottom()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  async onReady() {
    const userInfo: any = await getPersonalInformation()
    if (userInfo) {
      const {
        self_info,
        wechat,
        sex,
        work,
        attr,
        age,
        height,
        weight,
        annual_income,
        domicile_address,
        resident_address,
        marry_expect,
        is_house,
        is_marry,
        is_mate,
        is_feast,
        is_cohabit,
        is_cert,
        is_car,
        head_img,
        child_plan,
        relatives_plan,
        marry_plan,
        degree,
        nickname
      } = userInfo
      this.setData({
        head_img,
        age,
        height,
        weight,
        resident_address,
        domicile_address,
        attr,
        sex,
        work,
        degree,
        annual_income,
        is_house,
        is_car,
        is_marry,
        is_mate,
        is_feast,
        is_cert,
        child_plan,
        marry_plan,
        relatives_plan,
        is_cohabit,
        self_info,
        marry_expect,
        wechat,
        nickname
      })
    } else {
      wx.showToast({
        icon: 'error',
        title: '获取用户信息失败',
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
})
